<?php
class ActivityAction extends CommonAction{
	public function index() {
		$activity = M("Activity");
		
		$map['enddate']  = array("GT",time());
		$map['catetype'] = "Actdzp";
		$Actdzp          = $activity->where($map)->select();
		
		$map2['enddate']  = array("GT",time());
		$map2['catetype'] = "Actggk";
		$Actggk          = $activity->where($map2)->select();
		
		foreach($Actdzp as $k=>$v){
			$cat1['activityid'] = $v['id'];
			$dzp = M("Actdzp")->where($cat1)->find();
			$Actdzp[$k]['fileurl'] = find_thumb($dzp['solevar'],'chou','Actdzp');	
		}	
		foreach($Actggk as $k=>$v){
			$cat2['activityid'] = $v['id'];
			$ggk = M("Actggk")->where($cat2)->find();
			$Actggk[$k]['fileurl'] = find_thumb($ggk['solevar'],'chou','Actactggk');
		}
		
		$this->assign("Actdzp",$Actdzp);
		$this->assign("Actggk",$Actggk);
		$this->assign("empty1",L('aty_form_ele_none1'));
		$this->display();
	}
	
	public function vipcard(){
		$actvipcard = M("Actvipcard");
		
		$map['foundation'] = 1;
		$actvipcard        = $actvipcard->where($map)->find();
		
		$this->assign("actvipcard",$actvipcard);
		$this->display();	
	}
	
	public function yhj(){
		$yhj    = M("Actyhj");
		$yhjlog = M("Actyhjlog");
		
		$map['expires']   = array("GT",time());
		$data = $yhj->where($map)->select(); 
		
		foreach($data as $k=>$v){
			$count  = $yhjlog->where("activityid='".$data[$k]['id']."'")->count();
			$data[$k]['fileurl'] = find_thumb($v['solevar']);
			$data[$k]['attr']    = get_yhj_string($v['attr'],$v['type']);
			$data[$k]['count']   = $count;
		}
		
		
		$this->assign("empty",L('aty_form_ele_none2'));
		$this->assign("yhj",$data);
		$this->display();	
	}
	
	public function rotate(){
		$Activity = M("Activity");
		$Actdzp = M("Actdzp");
		$time = time();
		$where = "";
		$where .= "$time between begindate and enddate";
		$where .= " and catetype='Actdzp'";
		$Act = $Activity->where($where)->find();
		$Dzp = $Actdzp->where("id=$Act[id]")->find();
		
		if(!$Dzp) {
			$this->assign("jumpUrl",U('/Index/index'));   
			$this->success(L('aty_form_ele_dzpend'));
			exit();
		}
		$sn = create_id();
		$this->assign("sn",$sn);
		$this->assign("Act",$Act);
		$this->assign("Dzp",$Dzp);
		$this->display();	
	}
	
	public function getdzp() {
		$id = $this->_get("id");

		$Actdzp = M("Actdzp");
		$Dzp = $Actdzp->find($id);
		
		$rand = rand(0,$Dzp['totalnums']);

		switch($rand) {
			case 1:
				$arr = array('num'=>1,'prize'=>L('aty_form_ele_price'));
				echo json_encode($arr);
			break;
			case 2:
				$arr = array('num'=>2,'prize'=>L('aty_form_ele_award2'));
				echo json_encode($arr);
			break;
			case 3:
				$arr = array('num'=>3,'prize'=>L('aty_form_ele_award3'));
				echo json_encode($arr);
			break;
			default:
				$arr = array('num'=>0,'prize'=>L('aty_form_ele_thanks1'));
				echo json_encode($arr);
			break;
		}
	}
	
	public function dzp_bak() {
		$id = $this->_get("id");

		$Actdzp = M("Actdzp");
		$Dzp = $Actdzp->find($id);
		
		$rand = rand(0,$Dzp['totalnums']);

		switch($rand) {
			case 1:
				$arr = array('num'=>1,'prize'=>L('aty_form_ele_price'));
				echo json_encode($arr);
			break;
			case 2:
				$arr = array('num'=>2,'prize'=>L('aty_form_ele_award2'));
				echo json_encode($arr);
			break;
			case 3:
				$arr = array('num'=>3,'prize'=>L('aty_form_ele_award3'));
				echo json_encode($arr);
			break;
			default:
				$arr = array('num'=>0,'prize'=>L('aty_form_ele_thanks1'));
				echo json_encode($arr);
			break;
		}
	}
	
	public function adddzplog() {
		$Actdzplog = M("Actdzplog");
		$data['sn'] = $this->_post("sn");
		$data['level'] = $this->_post("level");
		$data['activityid'] = $this->_post("activityid");
		$data['username'] = $this->_post("username");
		$data['mobile']   = $this->_post("mobile");
		$data['addtime']   = time();
		$Actdzplog->add($data);
		$this->redirect('/Activity/rotate');
	}
	
	public function card() {
		$Activity = M("Activity");
		$Actggk = M("Actggk");
		$time = time();
		$where = "";
		$where .= "$time between begindate and enddate";
		$where .= " and catetype='Actggk'";
		$Act = $Activity->where($where)->find();
		$Ggk = $Actggk->where("id=$Act[id]")->find();
		
		if(!$Ggk) {
			$this->assign("jumpUrl",U('/Index/index'));   
			$this->success(L('aty_form_ele_ggkend'));
			exit();
		}

		$num = rand(1,$Ggk['totalnums']);
		
		switch($num) {
			case 1:
				$award = L('aty_form_ele_price');
			break;
			case 2:
				$award = L('aty_form_ele_award2');
			break;
			case 3:	
				$award = L('aty_form_ele_award3');
			break;
			default:
				$award = L('aty_form_ele_thanks2');
			break;
		}
		
		$sn = create_id();
		$this->assign("sn",$sn);
		$this->assign("num",$num);
		$this->assign("award",$award);
		$this->assign("Act",$Act);
		$this->assign("Ggk",$Ggk);
		$this->display();
	}
	
	public function addggklog() {
		if($this->_post("username")=='') {
			echo -1;
			exit();	
		}
		
		if($this->_post("mobile")=='') {
			echo -2;
			exit();	
		}
		$Addggklog          = M("Actggklog");
		$data['sn']         = $this->_post("sn");
		$data['level']      = $this->_post("level");
		$data['activityid'] = $this->_get("id");
		$data['username']   = $this->_post("username");
		$data['mobile']     = $this->_post("mobile");
		$data['addtime']    = time();
		$flag 				= $Addggklog->add($data);
		if($flag) {
			echo 1;
			exit();	
		} else {
			echo 0;
			exit();	
		}
	}
}
?>